import Vue from 'vue'  
import Router from 'vue-router'  
import CampusBinhai from '@/components/CampusBinhai'  
import CampusChashan from '@/components/CampusChashan'  
import CampusYueqing from '@/components/CampusYueqing'  
import LoginFrom from '@/components/LoginFrom'  
import StudentSystem from '@/components/StudentSystem'  

Vue.use(Router)  

const router = new Router({  
  routes: [  
    {  
      path: '/',  
      name: 'CampusBinhai',  
      component: CampusBinhai  
    },  
    {  
      path: '/chashan',  
      name: 'CampusChashan',  
      component: CampusChashan  
    },  
    {  
      path: '/yueqing',  
      name: 'CampusYueqing',  
      component: CampusYueqing  
    },  
    {  
      path: '/login',  
      name: 'LoginFrom',  
      component: LoginFrom  
    },  
    {  
      path: '/student-system',  
      name: 'StudentSystem',  
      component: StudentSystem,  
      meta: { requiresAuth: true } // 用于路由守卫  
    }  
  ]  
})  

// 添加路由守卫  
router.beforeEach((to, from, next) => {  
  if (to.matched.some(record => record.meta.requiresAuth)) {  
    // 此处检查登录状态，比如检查localStorage或者sessionStorage中的token  
    // 这里我们简单地使用固定值  
    const isLoggedIn = localStorage.getItem('user') === 'admin';  
    if (!isLoggedIn) {  
      next({  
        path: '/login',  
        query: { redirect: to.fullPath }  
      });  
    } else {  
      next();  
    }  
  } else {  
    next(); // 确保一定要调用 next()  
  }  
});  

export default router;